cmake_minimum_required(VERSION 2.8)
project(PWave)

set(COMPILER_CODE SW_ACC) # SW_ATHREAD, SW_ACC, GNU, INTEL


INCLUDE_DIRECTORIES(src/miscellaneous/h)
INCLUDE_DIRECTORIES(src/base/h)
INCLUDE_DIRECTORIES(src/envs/h)
INCLUDE_DIRECTORIES(src/stencil_math/x86/h)
INCLUDE_DIRECTORIES(src/pwave/h)

AUX_SOURCE_DIRECTORY(src/miscellaneous/cpp SRC_MIS)
AUX_SOURCE_DIRECTORY(src/base/cpp SRC_BASE)
AUX_SOURCE_DIRECTORY(src/stencil_math/x86/cpp SRC_STENCIL_MATH)
AUX_SOURCE_DIRECTORY(src/pwave/cpp SRC_PWAVE)

FILE(GLOB_RECURSE HEAD_MST src/miscellaneous/h/*.h
                           src/base/h/*.h
                           src/stencil_math/x86/h/*.h
                           src/envs/h/*.h
                           src/pwave/h/*.h)


# begin including source code and dependencies

if(${COMPILER_CODE} STREQUAL "SW_ATHREAD")
    AUX_SOURCE_DIRECTORY(src/stencil_math/sw_athread SRC_STENCIL_MATH_SW_ATHREAD)

    add_executable(PWave ${HEAD_MST} ${SRC_BASE} ${SRC_MIS} ${SRC_STENCIL_MATH} ${SRC_PWAVE}
                   ${SRC_STENCIL_MATH_SW_ATHREAD})

elseif(${COMPILER_CODE} STREQUAL "SW_ACC")
    AUX_SOURCE_DIRECTORY(src/stencil_math/sw_openacc SRC_STENCIL_MATH_SW_OPENACC)

    add_executable(PWave ${HEAD_MST} ${SRC_BASE} ${SRC_MIS} ${SRC_STENCIL_MATH} ${SRC_PWAVE}
                   ${SRC_STENCIL_MATH_SW_OPENACC})

elseif(${COMPILER_CODE} STREQUAL "GNU" OR ${COMPILER_CODE} STREQUAL "INTEL")
    add_executable(PWave ${HEAD_MST} ${SRC_BASE} ${SRC_MIS} ${SRC_STENCIL_MATH} ${SRC_PWAVE})
endif()

# end including source code and dependencies





# begin setting compilers and compiling and linking process

if(${COMPILER_CODE} STREQUAL "SW_ATHREAD")   # build host code and link slave code


    add_definitions("-DUSE_SW_ATHREAD")
    add_definitions("-DUSE_MASTER_ONLY")
    # add_definitions("-D__DEBUG__")
    INCLUDE_DIRECTORIES(/usr/sw-mpp/mpi2/include)


    # specify compilers
    set(CMAKE_C_COMPILER sw5cc)
    set(CMAKE_CXX_COMPILER sw5CC)

    execute_process(COMMAND which ${CMAKE_C_COMPILER}
                    OUTPUT_VARIABLE CC_OUT)
    message(STATUS "${Blue}Actually using C Compiler: ${CC_OUT}${ColourReset}")

    execute_process(COMMAND which ${CMAKE_CXX_COMPILER}
                    OUTPUT_VARIABLE CXX_OUT)
    message(STATUS "${Blue}Actually using CXX Compiler: ${CXX_OUT}${ColourReset}")



    # specify C_FLAGS and CXX_FLAGS
    set(CMAKE_C_FLAGS "-slave")
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3")


    set(CMAKE_CXX_FLAGS "-host")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3")


    # specify linker
    set(LINKER "mpiCC")

    execute_process(COMMAND which ${LINKER}
            OUTPUT_VARIABLE LINKER_OUT)
    message(STATUS "${Blue}Actually using Linker: ${LINKER_OUT}${ColourReset}")

    # specify linker flags
    set(CMAKE_EXE_LINKER_FLAGS "-hybrid")
    target_link_libraries(${PROJECT_NAME} stdc++)

    set(CMAKE_CXX_LINK_EXECUTABLE "${LINKER} <FLAGS> <CMAKE_CXX_LINK_FLAGS> <LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES>")


elseif(${COMPILER_CODE} STREQUAL "SW_ACC")

    add_definitions("-DUSE_SW_ACC")
    add_definitions("-DSIMPLE_ITER_OUT")

    # add_definitions("-D__DEBUG__")
    INCLUDE_DIRECTORIES(/usr/sw-mpp/mpi2/include)


    # specify compilers
    set(CMAKE_C_COMPILER swacc)
    set(CMAKE_CXX_COMPILER mpiCC)

    execute_process(COMMAND which ${CMAKE_C_COMPILER}
            OUTPUT_VARIABLE CC_OUT)
    message(STATUS "${Blue}Actually using C Compiler: ${CC_OUT}${ColourReset}")

    execute_process(COMMAND which ${CMAKE_CXX_COMPILER}
            OUTPUT_VARIABLE CXX_OUT)
    message(STATUS "${Blue}Actually using CXX Compiler: ${CXX_OUT}${ColourReset}")



    # specify C_FLAGS and CXX_FLAGS
    set(CMAKE_C_FLAGS "")
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3")


    set(CMAKE_CXX_FLAGS "")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3")


    # specify linker
    set(LINKER "swaCC")

    execute_process(COMMAND which ${LINKER}
            OUTPUT_VARIABLE LINKER_OUT)
    message(STATUS "${Blue}Actually using Linker: ${LINKER_OUT}${ColourReset}")

    # specify linker flags
    target_link_libraries(${PROJECT_NAME} stdc++)

    set(CMAKE_CXX_LINK_EXECUTABLE "${LINKER} <FLAGS> <CMAKE_CXX_LINK_FLAGS> <LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES>")


elseif(${COMPILER_CODE} STREQUAL "INTEL")   # build with intel compilers on servers


    find_package(OpenMP)
    set(CMAKE_CXX_FLAGS  "${CMAKE_CXX_FLAGS} -qopenmp -O3 -Ofast -xHost -funroll-loops -fno-fnalias")
    add_definitions("-DUSE_INTEL_COMPILER")
    add_definitions("-DSIMPLE_ITER_OUT")



elseif(${COMPILER_CODE} STREQUAL "GNU")       # build with GNU compilers (e.g., locally)
    find_package(OpenMP)
    add_definitions("-DUSE_GNU_COMPILER")
    set(CMAKE_CXX_FLAGS  "${CMAKE_CXX_FLAGS} -fopenmp -O3 -Ofast -funroll-loops")
endif()

# end setting compilers and compiling and linking process
